package com.muyu.system.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.muyu.common.security.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author lht
 */
@Component
public class MyBatisObjectHandler implements MetaObjectHandler {


    public static final String CREATED_BY = "createBy";
    public static final String CREATED_DATE = "createTime";
    public static final String UPDATE_BY = "updateBy";
    public static final String UPDATE_DATE = "updateTime";

    @Override
    public void insertFill(MetaObject metaObject) {
        Long userId = SecurityUtils.getUserId();
        this.strictInsertFill(metaObject, CREATED_BY, Long.class, userId == null ? 0L : userId);
        this.strictInsertFill(metaObject, CREATED_DATE, LocalDateTime.class, LocalDateTime.now());
    }


    @Override
    public void updateFill(MetaObject metaObject) {
        Long userId = SecurityUtils.getUserId();
        this.strictUpdateFill(metaObject, UPDATE_BY, Long.class, userId == null ? 0L : userId);
        this.strictUpdateFill(metaObject, UPDATE_DATE, LocalDateTime.class, LocalDateTime.now());
    }
}
